package wpwork.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wpwork.base.Pagination;
import wpwork.model.PersonByGroupDTO;
import wpwork.permission.entity.UserEntity;
import wpwork.permission.service.UserService;
import wpwork.redisEntity.UserInfo;
import wpwork.service.KshGroupService;
import wpwork.service.impl.DataInteractionService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/example")
public class RedisPushController {

    @Autowired
    private DataInteractionService dataInteractionService;

    @Autowired
    private KshGroupService groupService;

    @Autowired
    private UserService userService;


    // 保留原有的 testPublish 方法用于 HTTP 请求
    @PostMapping("/test-publish")
    public String testPublish(Pagination pagination) {
        // 原有业务逻辑不变
        return executeTestPublishLogic(pagination);
    }

    // 定时5分钟
//    @Scheduled(fixedRate = 300000)
    public String scheduledTestPublish() {
        // 创建默认的分页对象
        Pagination pagination = new Pagination();
        // 执行相同的业务逻辑
        return executeTestPublishLogic(pagination);
    }

    // 提取公共业务逻辑到单独的方法中
    private String executeTestPublishLogic(Pagination pagination) {
        // 将原来 testPublish 方法中的业务逻辑移到这里
        // 群组用户查询
        List<PersonByGroupDTO> personByGroupDTOS = groupService.selectPersonAndGroupName();
        List<UserInfo> userList = personByGroupDTOS.stream()
                .map(dto -> new UserInfo(dto.getGroupId(), dto.getGroupName()))
                .collect(Collectors.toList());

        //用户表直接查
//        List<UserInfo> userList = new ArrayList<>();
//        List<UserEntity> list = userService.getList(pagination, null, false, false, null, null);
//        userList.addAll(list.stream()
//                .map(entity -> new UserInfo(entity.getId(), entity.getAccount()))
//                .collect(Collectors.toList()));

        // 人员信息发布
        dataInteractionService.handlePersonInfo("747384603941027201", userList);

//        // 位置查询
//        dataInteractionService.handleLocationQuery("747384603941027201");
//
//        // 轨迹查询
//        dataInteractionService.handleTrackQuery("747384603941027201",
//                "2025-10-16 00:00:00", "2025-10-17 00:00:00");
//
//        // AI外呼
//        dataInteractionService.handleAICall("737384603941027201", "请往XX支援", userList);

        return "测试消息已发布";
    }

}